/*
 * This file is a part of the RMI Plugin for Eclipse tutorials.
 * Copyright (C) 2002-7 Genady Beryozkin
 */
package hifiSpeakers;

/**
 *@author Adam,Aqeel
 */

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RMIServerImpl extends UnicastRemoteObject implements RMIServer {
	
	/*
	 * something must set the name, check for other storages beforehand,
	 * with String[] registryComponents = registry.list();
	 */
	
	Registry registry;
	private String name;
	private static final long serialVersionUID = 5885886202424414094L;
	HifiPlayer p;

	
	public RMIServerImpl(String name) throws RemoteException {
		this.name = name;
	}
	
	public void streamFrom(String url) throws RemoteException{
		p = new HifiPlayer();
		System.out.println("we'll stream from " + url + " now");
		p.init(url);
	}
		
	public void begin() {
		try {
			registry = LocateRegistry.getRegistry();
			registry.rebind(name, new RMIServerImpl(name));
			System.out.println("started RMI server");
		} catch (RemoteException e) {
			System.err.println("Something happened trying to start RMI");
			e.printStackTrace();
			System.exit(-1);
		}
		System.out.println("The RMI server " + name +" is ready");
//		System.out.println("The RMI server is ready");
	}
	
	public void close(){
		try{
		registry.unbind(name);
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public void pauseStream(){
		p.stop();
	}
	
	public void playStream(){
		p.start();
	}
	
	public void stopStream(){
		p.destroy();
	}
}
